package Bitwise_operatio;

import java.util.HashMap;
import java.util.Map;

/**
 * Created with Intellij IDEA.
 * Description;
 * User:TQ02
 * Data:2024-01-30
 * Time:18:55
 * Text：消失的两个数
 */
public class Text3 {

//    hash表的方法
    public int[] missingTwo(int[] nums) {
        int k=0;
        int[] array=new int[2];
        HashMap<Integer,Integer> map=new HashMap<>();
        for(int i=1;i<=nums.length+2;i++){
            map.put(i,0);
        }
        for(int i=0;i<nums.length;i++){
            map.put(nums[i],1);
        }
        for(Map.Entry<Integer,Integer> entry: map.entrySet() ){
            if(entry.getValue()==0){
                array[k++]= entry.getKey();
            }
        }
        return array;
    }
//    异或
    public int[] missingTwo2(int[] nums) {
        int tmp=0;
        for(int x:nums) tmp^=x;
        for(int i=1;i<=nums.length+2;i++) tmp^=i;

        int diff=0;
        while(true){
            if(((tmp>>diff) & 1)==1)break;
            else diff++;
        }
        int[] ret=new int[2];
        for(int x:nums){
            if(((x>>diff)&1)==1)ret[1]^=x;
            else ret[0]^=x;
        }
        for(int i=1;i<=nums.length+2;i++){
            if(((i>>diff)&1)==1)ret[1]^=i;
            else ret[0]^=i;
        }
        return ret;
    }
    public static void main(String[] args) {

    }
}
